<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Type" content="application/force-download" />
<title>Appointment</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<link href="css/TestAppointment.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script src="js/OperateTool.js" type="text/javascript"></script>
<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
droplinemenu.buildmenu("nav")
</script>



<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>



<script type="text/javascript" language="javascript" src="js/prototype-1.js"></script>
<script type="text/javascript" language="javascript" src="js/prototype-base-extensions.js"></script>
<script type="text/javascript" language="javascript" src="js/prototype-date-extensions.js"></script>
<script type="text/javascript" language="javascript" src="js/behaviour.js"></script>
<script type="text/javascript" language="javascript" src="js/datepicker.js"></script>
<link rel="stylesheet" href="css/datepicker.css">
<script type="text/javascript" language="javascript" src="js/behaviors.js"></script>

<script type="text/javascript" language="javascript" src="js/jquery-ui-timepicker-addon.js"></script>


<script type="text/javascript">

	function uploadCallback(filename, ID, status){
	
		//alert(status);
		document.getElementById("oldFile" + ID).value = filename;
		document.getElementById("scannerFilePath" + ID).value = filename;
		document.getElementById("viewHref" + ID).href = filename;
		document.getElementById("scannerFile" + ID).outerHTML = document.getElementById("scannerFile" + ID).outerHTML;
		


		if( status == 0 ){
			document.getElementById("viewDiv" + ID).style.display = "inline";
			//alert("upload success!!!");
		}else if( status == 1 ){
			document.getElementById("viewDiv" + ID).style.display = "none";
			alert("file size larger than upload_max_filesize, upload error. old file  has already be removed!");
		}else if( status == 2 ){
			document.getElementById("viewDiv" + ID).style.display = "none";
			alert("file size larger than post_max_filesize, upload error. old file  has already be removed!");
		}else if( status == 3 ){
			document.getElementById("viewDiv" + ID).style.display = "none";
			alert("file part uplaod, upload error.old file  has already be removed!");
		}else if( status == 4 ){
			document.getElementById("viewDiv" + ID).style.display = "none";
			alert("delete success!");
		}else if( status == -1 ){
			document.getElementById("viewDiv" + ID).style.display = "none";
			alert("copy file error in server. old file has already be removed!");
		}else{
			document.getElementById("viewDiv" + ID).style.display = "none";
			alert("unknown reason, upload error. old file  has already be removed!");
		}


		
		changeFunc();

		

	}

	function generateHref(){
		var scannerFile1 = document.getElementById("scannerFilePath1").value;
		var scannerFile2 = document.getElementById("scannerFilePath2").value;
		var scannerFile3 = document.getElementById("scannerFilePath3").value;
		var scannerFile4 = document.getElementById("scannerFilePath4").value;
		var scannerFile5 = document.getElementById("scannerFilePath5").value;
		
		if( scannerFile1 == "" && scannerFile2 == "" && scannerFile3 == "" && scannerFile4 == "" && scannerFile5 == "" ){
			document.getElementById("viewDiv").style.display = "none";
		}else{
			document.getElementById("viewLink").href = "scanner_file_view.php?scannerFile1=" + scannerFile1.replace(/\//g, '_') + "&scannerFile2=" + scannerFile2 + "&scannerFile3=" + scannerFile3 + "&scannerFile4=" + scannerFile4 + "&scannerFile5=" + scannerFile5;
			document.getElementById("viewDiv").style.display = "block";

		}	


	}



	
	function unsignedCheckChange(){

		changeFunc();
		if( document.getElementById("isUnsigned").checked == true ){
			document.getElementById("appointmentDate").disabled = true;
			document.getElementById("appointmentDate").value = "";
			document.getElementById("appointmentTime").disabled = true;
			document.getElementById("appointmentTime").value = "";
			document.getElementById("unsignedType").disabled = false;
			
		}else{
			document.getElementById("appointmentDate").disabled = false;
			document.getElementById("appointmentTime").disabled = false;
			document.getElementById("unsignedType").disabled = true;
		}
	}
	
	
	function popViewAppointment(){
		var doctorName = "Geraldine";
		document.getElementById("doctorNameLabel").innerHTML = doctorName + "\'s ";
		jQuery("#ViewAppointmentDialog").dialog("open");
	}
	
	
	function viewAppointment(){
	
		
		

		var resultData;
		var viewDate = document.getElementById("viewDate").value;

		jQuery.ajax({type:'post', async:false,dataType:'json', url:"view_doctor_appointment.php", data:"viewDate=" + viewDate, success:function(result){
				resultData = result;
			},
			error:function (XMLHttpRequest, textStatus, errorThrown){
				alert(XMLHttpRequest.responseText);
			}
		});
		
		var divElement = document.getElementById("appointmentList");
		for( var i = divElement.childNodes.length - 1; i >= 0; i-- ){
			divElement.removeChild(divElement.childNodes[i]);
		}
		var tableObject = document.createElement("table");
		tableObject.setAttribute("style","width:500px;");
		for( var i = 0; i < resultData.data.length; i++ ){
				var childTr = document.createElement("tr");
				
				//alert("test" + i);
				var appointmentNoTd = document.createElement("td");
				appointmentNoTd.innerHTML = resultData.data[i].appointmentNo;
				appointmentNoTd.setAttribute("width","25%");
				childTr.appendChild(appointmentNoTd);
				//alert("qqq" + i);
				var patientNameTd = document.createElement("td");
				patientNameTd.innerHTML = resultData.data[i].patientFirstName + "  " + resultData.data[i].patientSurname;
				patientNameTd.setAttribute("width","25%");
				childTr.appendChild(patientNameTd);
				//alert("sss" + i);
				var dateTd = document.createElement("td");
				dateTd.innerHTML = resultData.data[i].dateSeen;
				dateTd.setAttribute("width","25%");
				childTr.appendChild(dateTd);
				//alert("ddd" + i);
				var timeTd = document.createElement("td");
				timeTd.innerHTML = resultData.data[i].timeSeen;
				timeTd.setAttribute("width","25%");
				childTr.appendChild(timeTd);
				//alert("hhh" + i);
				
				
				tableObject.appendChild(childTr);
				
		}
		if( resultData.data.length != 0 ){
			divElement.appendChild(tableObject);
		}else{
			var errorLabel = document.createElement("label");
			errorLabel.innerHTML = "No Appointment Info!!!";
			divElement.appendChild(errorLabel);
			
		}
		

		
		
	}

	function operateClinic(){
									var clinicNo = document.getElementById("clinicNoOpClinic").value;
							var clinicName = document.getElementById("clinicName").value;
							var clinicAddress =jQuery("#clinicAddress").val();
							var clinicDetail = jQuery("#detail").val();
							var postData = "clinicNo=" + clinicNo + "&clinicName=" + clinicName + "&clinicAddress=" + clinicAddress + "&detail=" + clinicDetail;

							
							jQuery.ajax({type:'post', async:false, dataType:'json', url:'add_clinic.php', data:postData, success:function(result){
							
									if( result.data.status == "success" ){
										alert(jQuery("#operateClinicButton").text() + " success!!!");
										if( jQuery("#operateClinicButton").text() == "Add" ){
											var clinicObject = document.getElementById("clinicNoOpClinic" );
											var option = new Option(result.data.clinicName, result.data.clinicNo);
											option.selected = true;
											clinicObject.options.add(option);
											jQuery("#AddClinicDialog").dialog("option", "buttons", [{text:"Update", id:"operateClinicButton", click:new Function("operateClinic()")}, {text:"Cancel", click:function(){jQuery(this).dialog("close");}}]);
											option = new Option(result.data.clinicName, result.data.clinicNo);
											option.selected = false;
										    document.getElementById("clinic").options.add(option);
										}
										jQuery("#AddClinicDialog").dialog("close");
									}else{
										
										alert(jQuery("#operateClinicButton").text() + " fail!!!" + result.data.msg);
									}
								},
								error:function (XMLHttpRequest, textStatus, errorThrown){
									alert(XMLHttpRequest.responseText);
								}
							});
	}

	jQuery(document).ready(function(){
		//initial the view appointment dialog
		jQuery("#ViewAppointmentDialog").dialog({
			autoOpen:false,
			width:700,
			height:600,
			modal:true,
			buttons:[
					{
						text:"OK",
						click:function(){
							jQuery(this).dialog("close");
						}
					}
				]		
		});

		//initial the operate clinic dialog
		jQuery("#AddClinicDialog").dialog({
			autoOpen:false,
			width:600,
			modal:true,
			buttons:[
					{
						text: "Add",
						id:"operateClinicButton",
						click:new Function("operateClinic()")
					},
					{
						text:"Cancel",
						click:function(){
							jQuery(this).dialog("close");
						}
					}
				]		
		});

		


		jQuery("#leaveDialog").dialog({
			autoOpen:false,
			modal:true,
			width:400,
			buttons:[
			         {
				         text:"Yes",
				         click:function(){
					         
				        	if( appointmentSubmit() == false ){
				        		jQuery(this).dialog("close");
					        	return;
				        	}
				        	isChange = false;
				        	previousPage();
				         }
				         
			         },
			         {
				         text:"No",
				         click:function(){
				        	 isChange = false;
				        	 previousPage();
				         }
			         },
			         {
				         text:"Cancel",
				         click:function(){
					         jQuery(this).dialog("close");
				         }
			         }
					]
			
		});

		jQuery("#appointmentTime").timepicker({
			addSliderAccess:true,
			sliderAccessArgs:{touchonly: true},
			controlType:'select'
		});
		
		var datepicker_CurrentInput;  
        jQuery(".ui-datepicker-buttonpane button.ui-datepicker-close").live("click", function (){  
            datepicker_CurrentInput.value = "";  
            datepicker_CurrentInput = null;
        });  



		jQuery("#appointmentDate").datepicker({
			dateFormat:'dd/mm/yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043',
			showButtonPanel: true, 
			closeText: 'clear', 
			beforeShow: function (input, inst) { datepicker_CurrentInput = input;},
			onSelect:function(){datepicker_CurrentInput = null;}
		});

		jQuery("#viewDate").datepicker({
			dateFormat:'dd/mm/yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043',
			showButtonPanel: true, 
			closeText: 'clear', 
			beforeShow: function (input, inst) { datepicker_CurrentInput = input;},
			onSelect:function(){datepicker_CurrentInput = null;}
		});






		//if( document.getElementById("visitOperate").getAttribute("disabled") == "disabled" ){
		//	document.getElementById("deleteButton").disabled = "disabled";
		//}

		//if( document.getElementById("visitOperate").disabled == "disabled" ){
		//	document.getElementById("visitOperate").disabled == "disabled";
		//}


	});
	



	function popAddClinic(){
		var clinicNoSelect = document.getElementById("clinic").value;
		var clinicDialogObject = document.getElementById("clinicNoOpClinic");
		for( var i = 0; i < clinicDialogObject.options.length; i++ ){
			if( clinicDialogObject.options[i].value == clinicNoSelect ){
				clinicDialogObject.options[i].selected = true;
				getClinicInfo();
			}
		}
		jQuery("#AddClinicDialog").dialog("open");
	}

	

	function getClinicInfo(){
		var clinicObject = document.getElementById("clinicNoOpClinic");
		var clinicNo = clinicObject.value;
		
		jQuery.ajax({type:'post', async:false,dataType:'json',  url:'get_clinic_info.php', data:"clinicNo=" + clinicNo, success:function(result){
				//jQuery("#clinicInDialog").attr("value", result.data.clinicNo);
				jQuery("#clinicName").attr("value", result.data.clinicName);
				jQuery("#clinicAddress").val(result.data.clinicAddress);
				jQuery("#detail").val(result.data.clinicDetail);
			},
			error:function (XMLHttpRequest, textStatus, errorThrown){
				alert(XMLHttpRequest.responseText);
				alert(textStatus);
				alert(errorThrown);
			}
		});
		if( clinicObject.value == 0 ){
			jQuery("#AddClinicDialog").dialog("option", "buttons", [{text:"Add", id:"operateClinicButton",click:new Function("operateClinic()")}, {text:"Cancel", click:function(){jQuery(this).dialog("close");}}]);
		}else{
			jQuery("#AddClinicDialog").dialog("option", "buttons", [{text:"Update", id:"operateClinicButton", click:new Function("operateClinic()")}, {text:"Cancel", click:function(){jQuery(this).dialog("close");}}]);
		}
		//jQuery("#operateClinicButton").addClass("ui-dialog-buttons");
	}




	function appointmentSubmit(){
		var isunsignedError = document.getElementById("isunsignedError");

		isunsignedError.innerHTML = "";


		var appointmentDateObject = document.getElementById("appointmentDate");
		var appointmentDate = appointmentDateObject.value;
		var appointmentTimeObject = document.getElementById("appointmentTime");
		var appointmentTime = appointmentTimeObject.value;

		//if( appointmentDate != "" ){
		//	var reg = new RegExp("^(((((0[13578]|1[02]))-(0[1-9]|[12][0-9]|3[01])|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02)-(0[1-9]|[1][0-9]|2[0-8]))-([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}))|(02-29-(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))))$");
		//	if( reg.test(appointmentDate) == false ){
		//		appointmentDateError.innerHTML = "date format not right!!!!";
		//		return false;
		//	}	
		//}

		//if( appointmentTime != "" ){
		//	var reg = new RegExp("^(([01][0-9])|(2[0-3])):[0-5][0-9]$");
		//	if( reg.test(appointmentTime) == false ){
		//		appointmentTimeError.innerHTML = "time format not right!!!";
		//		return false;
		//	}
		//}
		//judge whether unsigned datatime

		if( appointmentTime == "" || appointmentDate == "" ){
			if( document.getElementById("isUnsigned").checked == false ){
				isunsignedError.innerHTML = "select unsigned or input datetime!!!"
				return false;
			} 
		}

		var appointmentNo = document.getElementById("appointmentNo").value;
		var patientNo = document.getElementById("patientNo").value;

		var clinicObject = document.getElementById("clinic");
		var clinicNo = clinicObject.value;
		if( clinicNo == undefined || clinicNo == "" ){
			document.getElementById("clinicError").innerHTML = "select a clinic!!!!";
			clinicObject.focus();
			return false;
		}
		
		var isUnsigned;
		if( document.getElementById("isUnsigned").checked == true ){	
			isUnsigned = 1;
			
		}else{
			isUnsigned = 0;
		}


		var unsignedType = document.getElementById("unsignedType").value;
		
		
		var positionInQueue = document.getElementById("positionInQueue").value;

		var scannerFile1 = document.getElementById("scannerFilePath1").value;
		var scannerFile2 = document.getElementById("scannerFilePath2").value;
		var scannerFile3 = document.getElementById("scannerFilePath3").value;
		var scannerFile4 = document.getElementById("scannerFilePath4").value;
		var scannerFile5 = document.getElementById("scannerFilePath5").value;


		if( isChange == false ){
			alert("no change occur!!!");
			return;
		}
		
		var postData = "appointmentNo=" + appointmentNo + "&patientNo=" + patientNo + "&clinicNo=" + clinicNo + "&isUnsigned=" + isUnsigned 
						+ "&positionInQueue=" + positionInQueue + "&appointmentDate=" + appointmentDate + "&appointmentTime=" + appointmentTime + "&unsignedType=" + unsignedType + "&scannerFile1=" + scannerFile1 + "&scannerFile2=" + scannerFile2 + "&scannerFile3=" + scannerFile3 + "&scannerFile4=" + scannerFile4 + "&scannerFile5=" + scannerFile5;
			
		var isSuccess = false;
		jQuery.ajax({type:'post', async:false,dataType:'json', url:'appointment_op.php', data:postData, success:function(result){
				if(result.data.status == "success" ){
					var opVisitButtonObject = document.getElementById("visitOperate");
					var operateButtonObject = document.getElementById("operateButton");
					//alert(result.data.type + " success!!!");
					isChange = false;
					if( operateButtonObject.value == "Add" ){
						operateButtonObject.value = "Update";
						//window.location.href="list_appointment.php";
						window.location.href="patient_account.php?patientNo=" + patientNo;
						return;
					}
					document.getElementById("appointmentNo").value = result.data.appointmentNo;
					if( isUnsigned == 0 ){
						opVisitButtonObject.disabled = false;
					}else{
						opVisitButtonObject.disabled = true;
					}
					
					document.getElementById("deleteButton").disabled = false;
					
					isSuccess = true;
				}else{
					alert(result.data.type + " Fail!!! " + result.data.msg);
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert(XMLHttpRequest.responseText);
			}			
		});
		return isSuccess;
	}

	function gotoReviewPatient(type){
		var appointmentNo = document.getElementById("appointmentNo").value;
		if( type == 3 ){
			window.location.href="review_patient.php?appointmentNo=" + appointmentNo;
		}else{
			window.location.href="review_patient.php?appointmentNo=" + appointmentNo;
		}
				
	}


	function deleteObject(){
		var confirmResult = confirm("are you sure deleting the info of this appointment???");
		if( confirmResult == false ){
			return;	
		}

		
		var appointmentNo = document.getElementById("appointmentNo").value;
		jQuery.ajax({type:'post',async:false, dataType:'json',data:"No=" + appointmentNo + "&type=3", url:'delete_op.php', success:function(result){
				if( result.data.status == "success" ){
					alert("delete appointment success!!!");
					isChange = false;
					previousPage();
					document.getElementById("appointmentDate").value = "";
					document.getElementById("appointmentTime").value = "";
					document.getElementById("clinic").selectedIndex = 0;

					document.getElementById("isUnsigned").checked = true;

					document.getElementById("operateButton").value = "Add";
					document.getElementById("deleteButton").disabled = true;
					document.getElementById("visitOperate").disabled = true;
					
				}else{
					alert("delete appointment fail!!! " + result.data.msg);
				}
			}
		});
		
	}
	var isChange = false;

	function changeFunc(){
		isChange = true;
		document.getElementById("resultMsg").innerHTML = "";
	}

	jQuery(document).ready(function(){
		var lastURL = document.referrer;
		if( lastURL == "" ){
			SetCookie("appointmentPrevious", "list_appointment.php");
		}else if( lastURL.indexOf("review_patient.php") == -1 ){
			if( lastURL.indexOf("index.php") != -1 || lastURL.indexOf("patient_account") != -1 ){
				SetCookie("appointmentPrevious", lastURL);
			}else{
				SetCookie("appointmentPrevious", "list_appointment.php");
			}
		}else{
			if( getCookie("appointmentPrevious") == null || getCookie("appointmentPrevious") == "" ){
				SetCookie("appointmentPrevious", "list_appointment.php");
			}
		}
		
		
	});
	

	


	

	function previousPage(){
		if( isChange == true ){
			jQuery("#leaveDialog").dialog("open");
			return;
		}
		
		var lastPage = getCookie("appointmentPrevious");
		window.location.href = lastPage;
	}

	function leaveBefore(e){
		if( isChange == true ){
			return true;
		}
	}


	function downloadFile(){

		
	}




	
</script>

</head>

<body onbeforeunload="return leaveBefore(event);">
	<div id="container">
		<div id="header"></div>
		<?php include_once 'nav.php';?>
		<?php 
			$ini_array = parse_ini_file("config/config.ini");
			$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
			date_default_timezone_set($ini_array["default_timezone"]);
			mysqli_set_charset($conn, "utf8");
			ini_set('default_charset', "UTF-8");
			$appointmentNo = 0;
			$patientNo = 1;
			$type = 0;
			$patientFirstName = "";
			$patientSurname = "";
			$patientAddress = "";
			$birthDate = "";
			$clinicNo = 0;
			$positionInQueue = 0;
			$appointmentDate = "";
			$buttonText = "";
			$scannerFile1 = "";
			$scannerFile2 = "";
			$scannerFile3 = "";
			$scannerFile4 = "";
			$scannerFile5 = "";

			
			//check the valid of appointment_no then query the data from database
			//if appointment_no not exist, then add type , or update type
			if( isset($_GET["appointment_no"]) && strcmp(trim($_GET["appointment_no"]), "" ) != 0 &&  preg_match("/^\d+$/", $_GET["appointment_no"]) == true ){
				$appointmentNo = $_GET["appointment_no"];
				$appointmentNo = mysqli_real_escape_string($conn, $appointmentNo);
				$sql = "SELECT A.`appointment_no`, A.`appointment_datetime`, A.`position_in_queue`, A.`type`,P.`Patient_no`, P.`First_name`, 
						P.`Surname`, P.`address`, P.`Date_birth`, A.`clinicNo`, A.`scanner_file_1`, A.`scanner_file_2`, A.`scanner_file_3`, A.`scanner_file_4`, A.`scanner_file_5` FROM appointment AS A, patient AS P WHERE A.`appointment_no`=".$appointmentNo."
						 AND A.`Patient_no`=P.`Patient_no`";
				$result = mysqli_query($conn, $sql);
				if( $row = mysqli_fetch_array($result) ){
					$patientFirstName = $row["First_name"];
					$patientSurname = $row["Surname"];
					$patientAddress = $row["address"];
					if( strcmp($row["Date_birth"], "") != 0 ){
						$birthDate = date('d/m/Y',strtotime($row["Date_birth"]));
					}
					$clinicNo = $row["clinicNo"];
					$positionInQueue = $row["position_in_queue"];
					$patientNo = $row["Patient_no"];
					$type = $row["type"];
					$appointmentDate = $row["appointment_datetime"];
					$buttonText = "Update";
					$scannerFile1 = $row["scanner_file_1"];
					$scannerFile2 = $row["scanner_file_2"];
					$scannerFile3 = $row["scanner_file_3"];
					$scannerFile4 = $row["scanner_file_4"];
					$scannerFile5 = $row["scanner_file_5"];

				}else{
					echo "<script>alert('this appointment No. not exist in database!!!!');previousPage();</script>";
				}
			}else if(isset($_GET["patient_no"]) && preg_match("/^([0-9]+)$/", $_GET["patient_no"] == true)){
				$patientNo = $_GET["patient_no"];
				$patientNo = mysqli_real_escape_string($conn, $patientNo);
				$patientSql = "select First_name, Surname, address, Date_birth from patient where Patient_no=".$patientNo;
				$result = mysqli_query($conn, $patientSql);
				echo mysqli_error($conn);
				if( $row = mysqli_fetch_array($result)){
					$patientFirstName = $row["First_name"];
					$patientSurname = $row["Surname"];
					$patientAddress = $row["address"];
					if( strcmp($row["Date_birth"], "") != 0 ){
						$birthDate = date('d/m/Y',strtotime($row["Date_birth"]));
					}
				}else{
					echo "<script>alert('this patient has been deleted,please choose another patient!!');previousPage();</script>";
				}
				$buttonText = "Add";
			}else{
				echo "<script>alert('neither appointment No nor patient No find, or no format error!!!!');previousPage(); </script>";
			}
			
			
			
			
			date_default_timezone_set($ini_array["default_timezone"]);
			
			$clinicSql = "select * from clinic";
			$clinicResult = mysqli_query($conn, $clinicSql);
			
		
		?>
		
			<div id="main_content">
				<div id="appointmentData" style="width:800px;margin:40px auto;">
					<table style="width:780px;">
						<tr>
							<td style="text-align:right;width:40%;"><label>Appointment No:</label></td>
							<td style="width:60%;"><input type="text" name="appointmentNo" id="appointmentNo" readonly="readonly" <?php if( $type == 3 ) echo "disabled=\"disabled\""?> value="<?php echo $appointmentNo;?>" /><input type="hidden" value="<?php echo $patientNo?>" name="patientNo" id="patientNo"/></td>
						</tr>
						<tr>
							<td style="text-align:right;"><label>Patient First Name:</label></td>
							<td><input type="text" name="patientFirstName" value="<?php echo $patientFirstName?>" readonly="readonly" <?php if( $type == 3 ) echo "disabled=\"disabled\""?>/></td>
			
						</tr>
						<tr>
							<td style="text-align:right;"><label>Patient Surname:</label></td>
							<td><input type="text" name="patientSurname" value="<?php echo $patientSurname?>" readonly="readonly" <?php if( $type == 3 ) echo "disabled=\"disabled\""?>/></td>

						</tr>
						<tr>
							<td style="text-align:right;"><label>Patient Address:</label></td>
							<td rowspan="2"><textarea name="address" readonly="readonly" <?php if( $type == 3 ) echo "disabled=\"disabled\""?> style="height:6em;resize:none;"><?php echo $patientAddress?></textarea></td>
						</tr>
						<tr>
							<td></td>
		
						</tr>
						<tr>
							<td style="text-align:right;"><label>Date of Birth</label></td>
							<td><input type="text" name="dateBirth" value="<?php echo $birthDate;?>" readonly="readonly" <?php if( $type == 3 ) echo "disabled=\"disabled\""?> /></td>
	
						</tr>
						<tr>
							<td style="text-align:right;"><label>Clinic List:</label> </td>
							<td>
								<select name="clinic"  id="clinic" onclick="changeFunc()" >
									<?php 
										while( $row = mysqli_fetch_array($clinicResult)){
											$clinicNoInClinic = $row["Clinic_no"];
											if( $clinicNo == 0 ){
												$clinicNo = $clinicNoInClinic;
											}
											$clinicName = $row["Clinic_name"];
									?>
									<option  value="<?php echo $clinicNoInClinic?>"	<?php if( $clinicNo == $clinicNoInClinic){echo " selected=\"selected\"";}?>>
									<?php echo $clinicName?>
									</option>
									<?php }?>
								</select>
								<input type="button" value="Edit Clinic" onclick="popAddClinic()" />
								<label id="clinicError"></label>
							</td>
	
						</tr>
						<tr>
							<td style="text-align:right;"><label>Appointment Date:</label></td>
							<td>
								<input type="text" name="appointmentDate" id="appointmentDate" value="<?php if( strcmp($appointmentDate,"") != 0 )echo date("d/m/Y", strtotime($appointmentDate));?>" <?php if(  $type == 1) echo "disabled=\"disabled\""?> readonly="readonly" onchange="changeFunc()"/>
								<input type="button" value="View Doctors Appointment" onclick="popViewAppointment()" />
							</td>

						</tr>
						<tr>
							<td style="text-align:right;"><label>Appointment Time:</label></td>
							<td>
								<input type="text" name="appointmentTime" id="appointmentTime"  value="<?php if( strcmp($appointmentDate, "") != 0)echo date("H:i", strtotime($appointmentDate));?>" <?php if(  $type == 1) echo "disabled=\"disabled\""?> readonly="readonly" onchange="changeFunc()"/>
							</td>
		
						</tr>
						<tr>
							<td style="text-align:right;"><label>Unsigned Datetime</label></td>
							<td>
							<input type="checkbox" name="isUnsigned" id="isUnsigned" <?php if( $type == 1 ){ echo "checked=\"checked\"";} ?> <?php if( $type == 3 ){ echo "disabled=\"disabled\""; ?>onchange="unsignedCheckChange()" />
								<span id="isunsignedError"></span>
							</td>
				
						</tr>
						<tr>
							<td style="text-align:right;"><label>Unsigned appointment type:</label></td>
							<td>
								<select name="unsignedType" id="unsignedType" <?php if( $type != 1 ) echo "disabled=\"disabled\"";?>>
									<option value="1">typeOne</option>
									<option value="2">typeTwo</option>
									<option value="3">typeThree</option>
								</select>
							</td>
				
						</tr>
						<tr>
							<td style="text-align:right;"><label>Position in Queue(New Only):</label></td>
							<td><input type="text" name="positionInQueue" id="positionInQueue" value="<?php echo $positionInQueue?>" readonly="readonly" /></td>
			
						</tr>
						<tr>
							<td style="text-align:right;"><label>Visited</label></td>
							<td><input type="checkbox" name="isVisit" <?php if( $type == 3 ){echo "checked=\"checked\""; }?> disabled="disabled" value="checkbox"/></td>
	
						</tr>
						<tr>
							<td></td>
							<td>
								<form enctype="multipart/form-data" action="upload_scannerfile.php" method="post" target="hidden_frame">
									<input type="hidden" name="ID" value="1" />
									<input type="hidden" name="oldFile" id="oldFile1" />
									<input type="file" name="scannerFile" id="scannerFile1" />
									<input type="hidden" name="funcName" id="funcName2" value="uploadCallback" />
									<input type="submit" name="upfile" value="upload scanned file" />
									<div id="viewDiv1" style="display:<?php if( $scannerFile1 != "" ) echo "inline"; else echo "none";?>" ><a href="<?php echo $scannerFile1 ?>" target="_blank" id="viewHref1">view</a></div>
								</form>
							</td>
	
						</tr>
						<tr>
							<td></td>
							<td>
								<form enctype="multipart/form-data" action="upload_scannerfile.php" method="post" target="hidden_frame">
									<input type="hidden" name="ID" value="2" />
									<input type="hidden" name="oldFile" id="oldFile2" />
									<input type="file" name="scannerFile" id="scannerFile2" />
									<input type="hidden" name="funcName" id="funcName2" value="uploadCallback" />
									<input type="submit" name="upfile" value="upload scanned file" />
									<div id="viewDiv2" style="display:<?php if( $scannerFile2 != "" ) echo "inline"; else echo "none";?>" ><a href="<?php echo $scannerFile2 ?>" target="_blank" id="viewHref2">view</a></div>
								</form>
							</td>
	
						</tr>
						<tr>
							<td></td>
							<td>
								<form enctype="multipart/form-data" action="upload_scannerfile.php" method="post" target="hidden_frame">
									<input type="hidden" name="ID" value="3" />
									<input type="hidden" name="oldFile" id="oldFile3" />
									<input type="file" name="scannerFile" id="scannerFile3" />
									<input type="hidden" name="funcName" id="funcName3" value="uploadCallback" />
									<input type="submit" name="upfile" value="upload scanned file" />
									<div id="viewDiv3" style="display:<?php if( $scannerFile3 != "" ) echo "inline"; else echo "none";?>" ><a href="<?php echo $scannerFile3 ?>" target="_blank" id="viewHref3">view</a></div>
								</form>
							</td>
	
						</tr>
						<tr>
							<td></td>
							<td>
								<form enctype="multipart/form-data" action="upload_scannerfile.php" method="post" target="hidden_frame">
									<input type="hidden" name="ID" value="4" />
									<input type="hidden" name="oldFile" id="oldFile4" />
									<input type="file" name="scannerFile" id="scannerFile4" />
									<input type="hidden" name="funcName" id="funcName4" value="uploadCallback" />
									<input type="submit" name="upfile" value="upload scanned file"/>
									<div id="viewDiv4" style="display:<?php if( $scannerFile4 != "" ) echo "inline"; else echo "none";?>" ><a href="<?php echo $scannerFile4 ?>" target="_blank" id="viewHref4">view</a></div>
								</form>
							</td>
	
						</tr>
						<tr>
							<td></td>
							<td>
								<form enctype="multipart/form-data" action="upload_scannerfile.php" method="post" target="hidden_frame">
									<input type="hidden" name="ID" value="5" />
									<input type="hidden" name="oldFile" id="oldFile5" />
									<input type="file" name="scannerFile" id="scannerFile5" />
									<input type="hidden" name="funcName" id="funcName5" value="uploadCallback" />
									<input type="submit" name="upfile" value="upload scanned file" />
									<div id="viewDiv5" style="display:<?php if( $scannerFile5 != "" ) echo "inline"; else echo "none";?>" ><a href="<?php echo $scannerFile5 ?>" target="_blank" id="viewHref5">view</a></div>
								</form>
							</td>
	
						</tr>
						
					</table>
				</div>

				<!-- store upload file path-->
				<input type="hidden" id="scannerFilePath1" value="<?php echo $scannerFile1 ?>" />
				<input type="hidden" id="scannerFilePath2" value="<?php echo $scannerFile2 ?>" />
				<input type="hidden" id="scannerFilePath3" value="<?php echo $scannerFile3 ?>" />
				<input type="hidden" id="scannerFilePath4" value="<?php echo $scannerFile4 ?>" />
				<input type="hidden" id="scannerFilePath5" value="<?php echo $scannerFile5 ?>" />


				<div id="operate" style="text-align:center;">
					<input type="button" value="<?php echo $buttonText?>" onclick="appointmentSubmit()" id="operateButton"/>
					<input type="button" id="deleteButton"  value="Delete" onclick="deleteObject()"  <?php if( strcmp($buttonText,"Add") == 0  ){echo "disabled=\"disabled\"";}?>/>
					<input type="button" value="Return" onclick="previousPage()"/> 
					<input type="button" id="visitOperate" value="<?php if( $type == 3 ){echo "View Visit";}else{echo "Add Visit";}?>" <?php if(strcmp($buttonText,"Add") == 0 || $type == 1){echo "disabled=\"disabled\"";}?>  onclick="gotoReviewPatient(<?php echo $type?>)" /><br />
					<label id="resultMsg"></label>
				</div>
					
				<iframe id="hidden_frame" name="hidden_frame" style="display:none"></iframe>
				
				
				
			</div>
			
		
		
		
		<?php 
			$clinicSql = "select Clinic_no, Clinic_name from clinic";
			$clinicResult = mysqli_query($conn, $clinicSql);
		?>
		<div id="AddClinicDialog" title="Operate CLinic" style="display:none"> 
			<div id="clinicDialogDiv" >
				<table style="width:500px;">
					<tr>
						<td style="width:50%;text-align:right;"><label>Clinic List:</label> </td>
						<td style="width:50%;text-align:left;">
							<select name="clinicNoOpClinic" id="clinicNoOpClinic" onchange="getClinicInfo()">
								<option value="0">Add Clinic</option>
								<?php 
									while( $row = mysqli_fetch_array($clinicResult)){
										$clinicNoInClinic = $row["Clinic_no"];
										if( $clinicNo == 0 ){
											$clinicNo = $clinicNoInClinic;
										}
										$clinicName = $row["Clinic_name"];
								?>
								<option  value="<?php echo $clinicNoInClinic?>">
								<?php echo $clinicName?>
								</option>
								<?php }?>
							</select>
						</td>
					</tr>
					<tr>
						<td style="width:50%;text-align:right;"><label>Clinic Name:</label></td>
						<td style="width:50%;text-align:left;"><input type="text" name="clinicName" id="clinicName"/></td>
					</tr>
					<tr>
						<td style="width:50%;text-align:right;vertical-align:top;"><label>Clinic Address:</label></td>
						<td style="width:50%;text-align:left;" ><textarea name="clinicAddress" id="clinicAddress" style="resize:none;height:5em;"></textarea></td>
					</tr>
		
					<tr>
						<td style="width:50%;text-align:right;vertical-align:top;"><label>Clinic Detail:</label></td>
						<td style="width:50%;text-align:left;" rowspan="3"><textarea name="detail" id="detail" style="resize:none;height:5em;"></textarea></td>
					</tr>
		
				</table>
			</div>
			
			
			
			<input type="hidden" value="0" name="clinicNo" id="clinicInDialog"/>
			<label id="addMsg"></label>
		</div>
		
		
		<div id="ViewAppointmentDialog" title="View Doctor Appointment" style="display:none">
			<div id="viewDataDiv">
				<h2 style="display:inline-block"><label id="doctorNameLabel"></label><label>Appointment List</label></h2>
				<div style="display:inline-block;margin-left:10px;"><input type="text" id="viewDate" readonly="readonly"/><input type="button" onclick="viewAppointment()" value="view"/></div>
				<div style="width:120px;display:inline-block"><label>No.</label></div>
				<div style="width:120px;display:inline-block"><label>Patient</label></div>
				<div style="width:120px;display:inline-block"><label>Date</label></div>
				<div style="width:120px;display:inline-block"><label>Time</label></div>
				<div id="appointmentList" style="height:350px;overflow:auto;">
				</div>
			</div>
			
		</div>
		
		
		<div id="leaveDialog" title="leave dialog" style="display:none">
			<label>the appointment data have change, do  you save before leave?</label>
		</div>
		<?php include_once 'footer.php';?>
	</div>
</body>
</html>
